﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TN_CMS.Admin.Helper.Common;
using TN_CMS.Admin.Models;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.Entities;

namespace TN_CMS.Admin.Controllers
{
    public class NewsController : TNController
    {
        private readonly INewsGroupRepository _newsGroupRepository;
        private readonly INewsRepository _newsRepository;
        private readonly ICommonRepository _commonRepository;

        public NewsController(
            INewsRepository newsRepository, 
            INewsGroupRepository newsGroupRepository, 
            ICommonRepository commonRepository)
        {
            this._newsGroupRepository = newsGroupRepository;
            this._newsRepository = newsRepository;
            this._commonRepository = commonRepository;
        }

        #region News

        public ActionResult Index()
        {
            var model = new News();

            ViewBag.LstGroups = new SelectList(this._newsGroupRepository.NewsGroups
                .Where(g => g.IsActive == true) .ToList(), "Id", "Name");

            return View(model);
        }

        [HttpPost]
        public ActionResult News(int[] arrId)
        {
            var ckText = Request.Form["editor1"];
            var result = Json(new { message = "Success", data = ckText });

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetListNews(int pageIndex, string orderClause)
        {
            var result = new CustomJsonResult();
            int total = 0;
            int pageSize = AdminHelper.GetPageSize();

            try
            {
                string whereClause = string.Empty;

                var lst = this._newsRepository.GetList(pageIndex, pageSize, out total, orderClause, whereClause);
                result.Result = lst;
                result.Optional = total;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetDetailNews(int id)
        {
            var result = new CustomJsonResult();

            try
            {
                var detail = new News();

                if (id != -1)
                {
                    detail = this._newsRepository.GetDetail(id);
                }
                else
                {
                    detail.Id = -1;
                    detail.CreatedOn = DateTime.Now;
                    detail.IsActive = true;
                }

                result.Result = new
                {
                    Id = detail.Id,
                    Keywords = detail.Keywords,
                    Title = detail.Title,
                    Summary = detail.Summary,
                    Content = detail.Content,
                    TotalViews = detail.TotalViews,
                    CreatedOn = detail.CreatedOn,
                    CreatedBy = detail.CreatedBy,
                    UpdatedOn = detail.UpdatedOn,
                    UpdatedBy = detail.UpdatedBy,
                    IsActive = detail.IsActive,
                    LstGroups = detail.NewsGroups.Select(g => g.Id)
                };
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManageNews(News detail)
        {
            var result = new CustomJsonResult();

            try
            {
                if (this._newsRepository.Save(detail, 1) > 0)
                {
                    // delete mapping
                    this._commonRepository.Delete("NewsNewsGroupsMapping", "NewsId", Convert.ToString(detail.Id));

                    // insert mapping
                    if (detail.LstGroups != null && detail.LstGroups.Count > 0)
                    {
                        foreach (int NewsGroupId in detail.LstGroups)
                        {
                            this._commonRepository.InsertMapping("NewsNewsGroupsMapping", "NewsId", "NewsGroupId", detail.Id, NewsGroupId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateActiveNews(int[] arrId, bool isActive)
        {
            var result = new CustomJsonResult();

            try
            {
                this._newsRepository.UpdateActive(arrId, isActive);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteNews(int[] arrId)
        {
            var result = new CustomJsonResult();

            try
            {
                this._newsRepository.Delete(arrId);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        #endregion

        #region News group

        public ActionResult Group()
        {
            NewsGroup model = new NewsGroup();
            return View(model);
        }

        [HttpPost]
        public ActionResult Group(int[] arrId)
        {
            var ckText = Request.Form["editor1"];
            var result = Json(new { message = "Success", data = ckText });

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetListNewsGroup(int pageIndex, string orderClause)
        {
            var result = new CustomJsonResult();
            int total = 0;
            int pageSize = AdminHelper.GetPageSize();

            try
            {
                string whereClause = string.Empty;

                var lst = this._newsGroupRepository.GetList(pageIndex, pageSize, out total, orderClause, whereClause);
                result.Result = lst;
                result.Optional = total;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetDetailNewsGroup(int id)
        {
            var result = new CustomJsonResult();

            try
            {
                var detail = new NewsGroup();

                if (id != -1)
                {
                    detail = this._newsGroupRepository.GetDetail(id);
                }
                else
                {
                    detail.Id = -1;
                    detail.CreatedOn = DateTime.Now;
                    detail.IsActive = true;
                }

                result.Result = new
                {
                    Id = detail.Id,
                    Key = detail.Key,
                    Name = detail.Name,
                    Description = detail.Description,
                    TotalViews = detail.TotalViews,
                    CreatedOn = detail.CreatedOn,
                    CreatedBy = detail.CreatedBy,
                    UpdatedOn = detail.UpdatedOn,
                    UpdatedBy = detail.UpdatedBy,
                    IsActive = detail.IsActive,
                };
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult ManageNewsGroup(NewsGroup detail)
        {
            var result = new CustomJsonResult();

            try
            {
                this._newsGroupRepository.Save(detail, 1);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult UpdateActiveNewsGroup(int[] arrId, bool isActive)
        {
            var result = new CustomJsonResult();

            try
            {
                this._newsGroupRepository.UpdateActive(arrId, isActive);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteNewsGroup(int[] arrId)
        {
            var result = new CustomJsonResult();

            try
            {
                this._newsGroupRepository.Delete(arrId);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return Json(result);
        }

        //public ActionResult UploadImage(string folder)
        //{
        //    string image = MediaHelper.GetImage("test", "image.jpg");
        //    // Check if the request contains multipart/form-data.
        //}

    #endregion
    }
}
